import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;


public class oldclient {
	
	InetAddress serveraddr;
	Integer port;
	BufferedReader scriptfile;
	Socket socket;
	private static Logger log;
	
	public oldclient(InetAddress serveraddr, Integer port, BufferedReader scriptfile)
	{
		this.serveraddr = serveraddr;
		this.port = port; 
		this.scriptfile = scriptfile;
	}
	
	public void run() throws IOException
	{
		socket = new Socket(serveraddr, port);
		log.info("Connected to Server: " + socket);
		int queryNum = 0;	
		
		BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream()));
	    PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
	    
	    /*Handshake*/
	    out.println("HELLO");
	    out.flush();
	    String strout = in.readLine();
	    if(!strout.equalsIgnoreCase("HI"))
	    	{
	    		log.error("Uncorrect Server Response");
	    		return;
	    	}
	    
	    while(true)
	    {
	    		
	    	System.out.print("Command>");
	    	
	    	String strin = scriptfile.readLine();
	    	if(strin == null)
	    			break;
	    	strin = strin.trim();
	    	if(strin.equalsIgnoreCase("END") || strin.equalsIgnoreCase("BYE"))
	    		break;
	    	
	    	if(strin.contains("%"))
	    	{
	    		strin = strin.substring(0, strin.indexOf('%'));
	    	}
	    	
	    	if(strin.length() == 0)
	    		continue;
	    	
	    	String cmd[] = strin.split(" ");
	    	
	    	if(cmd[0].equalsIgnoreCase("QUERY"))
	    		{
	    			queryNum++;
	    			String query = "";
	    			for(int i =1 ; i < cmd.length;i++ )
	    				query += " " +cmd[i];
	    		  
	    			strin = "QUERY " + queryNum + " " + query;
	    		}
	    	
	    	out.println(strin); 
	    	out.flush();
	    	strout = in.readLine();
	    	
	    	String servList[] = strout.split(" ");
	    	System.out.println("Server Response:" + strout);
	    	
	    	if(servList[0].equalsIgnoreCase("QUERY-REPLY"))
	    	{
	    		int results = Integer.parseInt(servList[2]);
	    		
	    		for(int i=0; i < results; i++)
	    		{
	    			String queryresult = in.readLine();
	    			System.out.println("Query Result: " + queryresult);
	    		}
	    	}
	    }
	    
	    out.println("BYE");
	    out.flush();
	    strout = in.readLine(); /* We dont need to process this. */
		
	}
	
	
	public static void main(String[] args) {
		
		log = Logger.getLogger();
		if (args.length < 2){
			System.out.println("Usage java client server port [scriptfile] ");
			System.exit(1);
		}
		
		try {
			InetAddress addr = InetAddress.getByName(args[0]);
			Integer port = Integer.parseInt(args[1]);
			BufferedReader reader;
			
			if (args.length >= 3)
				reader = new BufferedReader(new FileReader(args[2]));
			else reader = new BufferedReader( new InputStreamReader(System.in));
			
			oldclient client = new oldclient(addr, port, reader);
			client.run();
			
		}catch (Exception e)
		{
			log.error("Cought Exception : "+e);
		}
	}
}
